<?php

namespace Kanin\Games\app;

use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;

class Distribute {

	/**
	 * 路由
	 */
	const ROUTE = [
		'0x101' => [UserController::class, 'login'],
		'0x103' => [UserController::class, 'upLevel'],
	];

	/**
	 * 不用授权
	 */
	const NO_AUTH = [
		0x101
	];

	/**
	 * 路由分发
	 * @param \Swoole\WebSocket\Server $server
	 * @param \Swoole\WebSocket\Frame $frame
	 * @return mixed
	 */
	public function router(Server $server, Frame $frame) {
		$data = json_decode($frame->data, true);
		if (!isset($data['cmd'])) {
			return -1;
		}
		$cmd = '0x' . dechex($data['cmd']);
		$route = self::ROUTE[$cmd];
		$controller = new $route[0];
		return $controller->{$route[1]}($server, $frame);
	}
}